import './index.css';

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ConfigProvider } from 'antd';
import zh_CN from 'antd/locale/zh_CN';
import React from 'react';
import ReactDOM from 'react-dom/client';

import ErrorBoundary from '@/components/ErrorBoundary';

import App from './App';

// 创建 QueryClient 实例, 并配置defaultOptions选项
const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 0, // 数据立即过期
      retry: 1, // 请求失败重试次数
      retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000), // 指定重试延迟时间
      refetchOnWindowFocus: false, // 窗口重新聚焦时重新获取数据
      refetchOnReconnect: true, // 重新连接网络时重新获取数据
      refetchInterval: false, // 关闭轮询
      refetchIntervalInBackground: false, // 关闭后台轮询
    },
    mutations: {
      retry: 1, // 变更失败重试次数
      retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000), // 指定重试延迟时间
    },
  },
});

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <ErrorBoundary>
      <QueryClientProvider client={queryClient}>
        {/* antd 全局化配置 */}
        <ConfigProvider locale={zh_CN}>
          <App />
        </ConfigProvider>
      </QueryClientProvider>
    </ErrorBoundary>
  </React.StrictMode>,
);
